use "${datadir}\HTB_main", replace

keep if year>2015


****** Variables

egen net_income_total 	= rowtotal(main_borrower_net_income second_borrower_net_income other_borrowers_net_income)

drop if payment*1000 > net_income_total/12

gen PTInet2 			= (payment*1000) / (net_income_total/12) * 100

gen PTInet2_credcomm 	= (payment*1000) / (net_income_total/12 - totalcreditcommitments) * 100

gen PTInet2_credcommexp = (payment*1000) / (net_income_total/12 - totalcreditcommitments  ///
														- monthlypaymentforcommittedexpend - basichouseholdexpenditure)  * 100


mat T = J(8,6,.)
local col = 1
local row = 2

****** All
foreach x in PTI PTInet2  {
		
	sum `x', d // if htb_dummy==1, d
	mat T[`row',`col'] 		= `r(N)'
	mat T[`row',`col'+1] 	= `r(mean)'
	mat T[`row',`col'+2] 	= `r(sd)'
	mat T[`row',`col'+3] 	= `r(p1)'	// `r(p10)'
	mat T[`row',`col'+4] 	= `r(p50)' 
	mat T[`row',`col'+5] 	= `r(p99)' // `r(p90)'
					
	local row = `row'+1	
}
*

****** 3 Banks
preserve
	keep if lender_name == "X" |  lender_name == "Y" | lender_name == "Z"  // confidential information
	drop if payment*1000 + totalcreditcommitments + monthlypaymentforcommittedexpend + basichouseholdexpenditure  > net_income_total/12

	local row = `row'+1	
	foreach x in PTI PTInet2 PTInet2_credcomm PTInet2_credcommexp {
			
		sum `x', d // if htb_dummy==1, d
		mat T[`row',`col'] 		= `r(N)'
		mat T[`row',`col'+1] 	= `r(mean)'
		mat T[`row',`col'+2] 	= `r(sd)'
		mat T[`row',`col'+3] 	= `r(p1)' // `r(p10)'
		mat T[`row',`col'+4] 	= `r(p50)'
		mat T[`row',`col'+5] 	= `r(p99)' // `r(p90)'		
		
		local row = `row'+1	
		
	}
restore

mat rownames T =  	"Panel A - All" ///
						"\quad PTI (gross)" ///
						"\quad PTI (net)" ///
					"Panel B - Three banks"	///
						"\quad PTI (gross)" ///
						"\quad PTI (net)" ///
						"\quad PTI (net - other debt)" ///
						"\quad PTI (net - other debt - exp.)" 
					mat list T 
				
frmttable using "${tabledir}\PTI_grossnetexp.tex", tex fragment nocenter  ///
			statmat(T) sdec(1) varlabels replace ///
			ctitle(	"", "Obs." , "Mean" , "Sd" , "p1" , "Median" , "p99" ) //
